//
//  tranSumViewController.swift
//  NewLndd
//
//  Created by 邵禹曦 on 2020/1/7.
//  Copyright © 2020 辽宁当代. All rights reserved.
//

import UIKit

// MARK: - 成交汇总
class tranSumViewController: HomeApiViewController, UITableViewDelegate,UITableViewDataSource {

    // 定义数组  Use of undeclared type 'Status'  数组 : [模型Model]
    private lazy var ViewModels : [tranSum] = [tranSum]()
    @IBOutlet weak var TableView: UITableView!
    override func viewDidLoad() {
        super.viewDidLoad()
        showInfo()

    }
    
}


extension tranSumViewController {
    // 用户验证
    private func showInfo() {
           // GCD
           let group = DispatchGroup()
    
           // 从沙盒中获取参数
            var userPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
            userPath =  (userPath as NSString).appendingPathComponent("data.plist")
            user = NSKeyedUnarchiver.unarchiveObject(withFile: userPath) as? User
        
        Thread.sleep(forTimeInterval: 0.05)


           // 用户验证
           self.u = user?.U!
           self.retcode = user?.RETCODE!
           // 用户验证
           let str = [u,"101",retcode]
            
           //self.isLogin = self._network(str: str as! [String], group: group)
        group.enter()
        NetworkTools.share.check_user(code: str as Array<Any>) { (result, error) in
                      
                          if error != nil {
                              print("no no no")
                              return
                          }
                          

                          if Int(self._retcode(res: result!))! < 0 {
                              self.isLogin = false
                          } else {
                              self.isLogin = true
                          }
                          group.leave()
                      }

           group.notify(queue: DispatchQueue.main) {
               if !self.isLogin {
                   return self.setupVisitorView()
               } else {
                
                Thread.sleep(forTimeInterval: 0.05)
                    // 2.设置导航栏内容
                    self.tableShouInfo()
               }
            }
       }
}

extension tranSumViewController {
    // MARK: - TableViewShowInfo
    @objc private func tableShouInfo() {
        
        let str = [user?.U!,user?.RETCODE!]
        
        NetworkTools.share.trade_sum_query(code: str as Array<Any>) { (result, error) in
            
            if error != nil {
                print("no no no")
                return
            }
            

            
            // 获取可选类型中的数组
            guard let resultArray = result else {
                print("no")
                return
            }
            
            if let REP = resultArray["REP"] as? [String : AnyObject] {
                if let arr = REP["RESULT"] as? [String : AnyObject]   {
                    let code : Int = Int(arr["RETCODE"] as! String)!
                    if(Int(truncating: NSNumber(value: code)) < 0) {
                        if #available(iOS 13.0, *) {
                            self.showMsgbox(_message: "数据加载失败")
                            return self.setupVisitorView()
                        }
                    }
                }
            }
            
        
            
           
            
            if let REP = resultArray["REP"] as? [String : AnyObject] {
                if let arr = REP["RESULTLIST"] as? [String : AnyObject]   {
                    
                    
                   for temp in arr["REC"] as! NSArray {
                       let sta = tranSum(dict: temp as! [String : AnyObject])
                       self.ViewModels.append(sta)
                   }
                    // 刷新表格
                    self.TableView.reloadData()
                }
            }
            
        }
        
    }
    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.ViewModels.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell : tranSumViewCell = tableView.dequeueReusableCell(withIdentifier: "tranSumViewCellid") as! tranSumViewCell
        cell.ViewModel = ViewModels[indexPath.row]
        return cell
    }
    
    // cell高
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 280
    }
}
